package cn.sm1234.service.impl;

import cn.sm1234.dao.EmpRepository;
import cn.sm1234.domain.Emp;
import cn.sm1234.service.EmpService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import org.springframework.data.domain.Pageable;
import java.util.List;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {

    @Resource
    private EmpRepository empRepository;

    @Override
    @Cacheable(value = "emp")
    public List<Emp> findAll() {
        return empRepository.findAll();
    }

    @Override
    @Cacheable(value = "emp")  // 缓存返回值, 名字指定对象；默认参数值为KEY
    public Emp findById(Integer id) {
        return empRepository.findOne(id);
    }

    @Override
    @Cacheable(value = "emp")
    public Page<Emp> findByPage(Pageable pageable) {
        return empRepository.findAll(pageable);

    }

    @Override
    @CacheEvict(value = "emp", allEntries = true)  //驱逐该项，缓存的所有记录
    public void save(Emp emp) {
        empRepository.save(emp);
    }
}
